package git.soulbgm.controller;

import git.soulbgm.common.enums.Status;
import git.soulbgm.common.pojo.PageInfo;
import git.soulbgm.common.pojo.ResultData;
import git.soulbgm.common.pojo.ReturnCode;
import git.soulbgm.pojo.dto.RoleDto;
import git.soulbgm.pojo.entity.Role;
import git.soulbgm.security.pojo.LoginUser;
import git.soulbgm.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 角色管理接口
 *
 * @author SoulBGM
 * @date 2020-09-10 17:14:11
 */
@RequestMapping("role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("findById")
    public ResultData findById(Long id) {
        Role role = roleService.findById(id);
        return ResultData.getResultData(ReturnCode.SUCCESS, role);
    }

    @GetMapping("find")
    public ResultData find(RoleDto role) {
        PageInfo pageInfo = roleService.find(role.getPageNum(), role.getPageSize(), role);
        return ResultData.getResultData(ReturnCode.SUCCESS, pageInfo);
    }

    @PostMapping("add")
    public ResultData registered(@RequestBody RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        role.setDelFlag(Status.EXIST.code);
        role.setStatus(Status.NORMAL.code);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String createUser = "admin";
        if (authentication != null) {
            LoginUser userDetails = (LoginUser) authentication.getPrincipal();
            createUser = userDetails.getUsername();
        }
        role.setCreateUser(createUser);
        role.setUpdateUser(createUser);
        Role dbRole = roleService.add(role, roleDto.getMenuIdList());
        if (dbRole != null) {
            return ResultData.getResultData(ReturnCode.SUCCESS, role);
        } else {
            return ResultData.getResultData(ReturnCode.SAVE_FAIL);
        }
    }

    @PostMapping("update")
    public ResultData update(@RequestBody RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String updateUser = "admin";
        if (authentication != null) {
            LoginUser userDetails = (LoginUser) authentication.getPrincipal();
            updateUser = userDetails.getUsername();
        }
        role.setUpdateUser(updateUser);
        role.setUpdateTime(new Date());

        Boolean result = roleService.update(role, roleDto.getMenuIdList());
        if (result) {
            return ResultData.getResultData(ReturnCode.SUCCESS);
        } else {
            return ResultData.getResultData(ReturnCode.UPDATE_FAIL);
        }
    }

    @PostMapping("delete")
    public ResultData delete(String ids) {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        Boolean result = roleService.delete(idList);
        if (result) {
            return ResultData.getResultData(ReturnCode.SUCCESS);
        } else {
            return ResultData.getResultData(ReturnCode.REMOVE_FAIL);
        }
    }

}
